package be.jprojects.tests.filter;

import java.awt.Color;
import java.awt.Component;
import java.util.HashMap;

import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.UIDefaults;
import javax.swing.UIManager;

import be.jprojects.subscription.logic.City;
import be.jprojects.subscription.logic.Country;
import be.jprojects.subscription.logic.Person;
import be.jprojects.subscription.logic.Province;
import be.jprojects.subscription.logic.Street;
import be.jprojects.subscription.logic.interfaces.ICity;
import be.jprojects.subscription.logic.interfaces.IPerson;

public class FilterItemRenderer extends Object implements ListCellRenderer{
	private static Color listForeground,
	listEvenBackground, listOddBackground,
	listSelectionForeground, listSelectionBackground;
	
	private HashMap<String, ListCellRenderer> cellrenderers;
	
	static {
		UIDefaults uid = UIManager.getLookAndFeel().getDefaults();
		listForeground = uid.getColor("List.foreground");
		listEvenBackground = uid.getColor("List.background");
		listOddBackground = new Color(228, 237, 249);
		listSelectionForeground = uid.getColor("List.selectionForeground");
		listSelectionBackground = uid.getColor("List.selectionBackground");
	}
	
	
	public FilterItemRenderer() {
		cellrenderers = new HashMap<String, ListCellRenderer>();
		cellrenderers.put(Country.class.getName(), new CountryListCellRenderer());
		cellrenderers.put(City.class.getName(), new CityListCellRenderer());
		cellrenderers.put(Province.class.getName(), new ProvinceListCellRenderer());
		cellrenderers.put(Person.class.getName(), new PersonListCellRenderer());
		cellrenderers.put(Street.class.getName(), new StreetListCellRenderer());
	}
	
	
	
	@Override
	public Component getListCellRendererComponent(JList list, Object value,
			int index, boolean isSelected, boolean cellHasFocus) {
		
		ListCellRenderer renderer;
		if (value == null) {
			renderer = new EmptyListCellRenderer();
		} else {
			renderer = cellrenderers.get(value.getClass().getName());
		}
		if (renderer == null) {
			renderer = new EmptyListCellRenderer();
		}
		
		FilterItemPanel fip = (FilterItemPanel) renderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
		
		
		if (isSelected) {
			fip.setColors(listSelectionForeground, listSelectionBackground);
		} else {
			if (index%2==0)
				fip.setColors(listForeground, listEvenBackground);
			else
				fip.setColors(listForeground, listOddBackground);
		}
		
		return fip;
	}

}
